home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4820 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: sundog.tiac.net!smayo
  2. From: smayo@tiac.net (Scott Mayo)
  3. Newsgroups: comp.lang.c++
  4. Subject: Painted into a corner?
  5. Date: Thu, 01 Feb 1996 02:13:55 -0500
  6. Organization: The Internet Access Company
  7. Message-ID: <d5e2.smail.smayo@tiac.net>
  8. NNTP-Posting-Host: sunspot.tiac.net
  9.  
  10. I've stumbled into an awkward case while setting up a few classes with
  11. multiple inheritance, and I'm not sure how to strauighten it out. Advice
  12. appreciated.
  13.  
  14. I have a class, X, which is never going to be instantiated, but subclasses derived
  15. from it will be.  I have another class, Y, which might be instantiated someday,
  16. though at the moment it exists only to support the subclasses Y1, Y2, and Y3.
  17.  
  18. I need to create 3 new classes, X1, X2, and X3, which inherit as follows
  19. X1: X and Y1
  20. X2: X and Y2
  21. X3: X and Y3
  22. X4: X
  23.  
  24. Very simple. It also turns out that I always need to manipulate instances of
  25. Xn through pointers to X, and trust to virtual functions
  26. to get the properly X1ish, X2ish and X3ish behaviour I need as regards
  27. the behaviour of the Yish parts. X4 is willing to support dummy functions
  28. so that it looks Yish as well; I'd rather it didn't inherit Y directly
  29. as that would make it pointlessly bulky.
  30.  
  31. The problem, which the experienced folk here probably see already, is that
  32. if I create X *x, any time I try to do something like x->member_of_y(),
  33. I get told that member_of_y() isn't part of X. Well, no, of course it
  34. is not. I can't convince the compiler that any use of X* is going to refer
  35. to Xn, all of which will have some flavor of member_of_y().
  36.  
  37. The closest I've come to a workaround is to create, in X,
  38. virtual member_of_y(), and then in each of X1...
  39.  
  40. X1's member_of_y() {Y1::member_of_y();}
  41. X2's member_of_y() {Y2::member_of_y();}
  42.  
  43. This strikes me as incenstous. X know knows a lot more than it ought
  44. about this upcoming marriage to Y. What am I missing? It's not
  45. practical to change all X* pointers to Y*.
  46.